﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UseCase.Operations.cs" company="">
//   
// </copyright>
// <summary>
//   The use case.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.UseCases
{
	using System;
	using System.Threading.Tasks;

	/// <summary>
	///   The use case.
	/// </summary>
	public abstract partial class UseCase
	{
		#region Public Events

		/// <summary>
		///   The wait ended.
		/// </summary>
		//public event Action<IUseCase, IOperation> WaitEnded;
		public event Action<IUseCase> WaitEnded;

		/// <summary>
		///   The wait started.
		/// </summary>
		//public event Action<IUseCase, IOperation> WaitStarted;
		public event Action<IUseCase> WaitStarted;

		/// <summary>
		///   The wait progress changed.
		/// </summary>
		public event Action<IUseCase> WaitProgressChanged;

		#endregion

		// public virtual WaitHandler OnStartWait(IOperation operation)
		// {
		// 	if (WaitStarted != null)
		// 	{
		// 		WaitStarted(this, operation);
		// 	}
		// }

		// public WaitHandler OnStartWait(string text)
		// {
		// 	var operationService = Service.GetService<IOperationService>();
		// 	var operation = operationService.StartOperation(text);
		// 	StartWait(operation);
		// }

		// public virtual void OnEndWait()
		// {
		// 	if (WaitEnded != null)
		// 	{
		// 		WaitEnded(this, operation);
		// 	}
		// }
		#region Public Properties

		/// <summary>
		///   Gets a value indicating whether waiting.
		/// </summary>
		/// <exception cref="NotImplementedException"></exception>
		public bool Waiting
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		#endregion

		#region Public Methods and Operators

		protected void StartWait()
		{
			if (WaitStarted != null)
			{
				WaitStarted(this);
			}
		}

		protected void EndWait()
		{
			if (WaitEnded != null)
			{
				WaitEnded(this);
			}
		}

		public Task<T> DoAsync<T>(Func<T> func)
		{
			StartWait();
			var task = Task.Factory.StartNew<T>(func);
			task.ContinueWith(t => EndWait(), TaskScheduler.FromCurrentSynchronizationContext());
			return task;
		}

		public void CancelWait()
		{
		}
		#endregion
	}
}